﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FBWMS
{
    public partial class F_COM_Search_GroupID : Form
    {
        public F_COM_Search_GroupID()
        {
            InitializeComponent();
        }

        private DataTable dt_GroupID;

        private void P_CreateSearchCombo()
        {
            string str_Message = string.Empty;
            string str_Caption = string.Empty;
            try
            {
                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcStart, "P_CreateSearchCombo Start", "P_CreateSearchCombo");

                string str_Language = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();

                //0:受注伝票No、1:PicNo、2:FBNo、3:売上伝票NO、4:客注No
                DataSet ds = new DataSet();
                ds.Tables.Add();

                //列の追加
                ds.Tables[0].Columns.Add();
                ds.Tables[0].Columns.Add();

                ds.Tables[0].Rows.Add();
                
                ds.Tables[0].Rows[0][0] = "1";
                ds.Tables[0].Rows[0][1] = "Group ID";

                if (!G_CommonClassControl.G_Control_SetData_ComboBox(cmb_Search_Mode, ds, 1, 0))
                {
                    str_Language = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_BindComboBox);
                    MessageBox.Show(str_Language);
                    return;
                }

                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, "P_CreateSearchCombo End", "P_CreateSearchCombo");
            }
            catch (Exception ex)
            {
                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.Error, "P_CreateSearchCombo Error" + ex.Message, "P_CreateSearchCombo");

                //エラーメッセージ表示
                str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);
                MessageBox.Show("P_CreateSearchCombox" + ex.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool P_Check_Search()
        {
            string str_Message = string.Empty;
            string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);
            if (!G_CommonClassChk.G_Chk_Null(this.txt_Search_CD.Text))
            {
                str_Message = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Set_Search_Value);
                MessageBox.Show(str_Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txt_Search_CD.Focus();
                return false;
            }

            return true;
        }

        private void P_BindDataGrid(FarPoint.Win.Spread.SheetView sheet, DataTable dt)
        {
            if ((dt == null) || (dt.Rows.Count == 0))
            {
                sheet.RowCount = 0;
                return;
            }
            sheet.RowCount = dt.Rows.Count;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                sheet.Cells[i, 0].Tag = row["GP_NO"];
                sheet.Cells[i, 1].Value = row["GP_NO"];
                sheet.Cells[i, 2].Value = row["Reg_DT"];
                sheet.Cells[i, 3].Value = row["Upd_DT"];
                sheet.Cells[i, 4].Value = row["Reg_WorkCD"];
                sheet.Cells[i, 5].Value = row["Upd_WorkCD"];
                sheet.Cells[i, 6].Value = row["Flag"];
            }
        }

        private void P_Search()
        {


            string str_Search_Mode = cmb_Search_Mode.SelectedValue.ToString();
            string str_Search_CD = this.txt_Search_CD.Text.Trim();

            dt_GroupID = G_CommonClassBussniss.G_F_COM_Search_GroupID_GetData(str_Search_Mode, str_Search_CD);

            P_BindDataGrid(this.fpSpread1.ActiveSheet, this.dt_GroupID);
        }

        private void P_Clear()
        {

            this.cmb_Search_Mode.SelectedValue = "1";
            this.txt_Search_CD.Text = "";
            
            this.fpSpread1.ActiveSheet.RowCount = 0;

            if (this.dt_GroupID != null)
            {
                this.dt_GroupID.Clear();
            }
            this.txt_Search_CD.Focus();
        }

        private void F_COM_Search_GroupID_Load(object sender, EventArgs e)
        {
            try
            {
                P_CreateSearchCombo();

                this.fpSpread1.ActiveSheet.RowCount = 0;
                G_CommonDefine.G_txt_GroupID.Text = "";
                this.txt_Search_CD.Focus();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.P_Check_Search())
                {
                    return;
                }

                //待機画面の表示
                G_CommonClassOther.G_Other_WaitForm(true);

                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcStart, "btn_Search_Click Start", "btn_Search_Click");

                //検索処理
                this.P_Search();

                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, "btn_Search_Click End", "btn_Search_Click");
            }
            catch (Exception ex)
            {
                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.Error, "btn_Search_Click Error" + ex.Message, "btn_Search_Click");

                //エラーメッセージ表示
                string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);
                MessageBox.Show("btn_Search_Click" + ex.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (G_CommonDefine.G_F_COM_Waiting != null)
                {
                    //待機画面の解除
                    G_CommonClassOther.G_Other_WaitForm(false);
                }
            }
        }

        private void fpSpread1_ButtonClicked(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
        {
            try
            {
                if (this.fpSpread1.ActiveSheet.Cells[e.Row, 1].Value == null)
                    return;
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcStart, "mr_F_COM_Search_FB_NO_SUB_ButtonClicked Start", "mr_F_COM_Search_FB_NO_SUB_ButtonClicked");

                G_CommonDefine.G_txt_GroupID.Text = this.fpSpread1.ActiveSheet.Cells[e.Row, 1].Value.ToString();
                this.Close();

                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, "mr_F_COM_Search_FB_NO_SUB_ButtonClicked End", "mr_F_COM_Search_FB_NO_SUB_ButtonClicked");
            }
            catch (Exception err)
            {

                string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.Error, "mr_F_COM_Search_FB_NO_SUB_ButtonClicked" + err.Message, "mr_F_COM_Search_FB_NO_SUB_ButtonClicked");
                MessageBox.Show(err.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            try
            {
                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcStart, "btn_Clear_Click Start", "btn_Clear_Click");

                //クリア処理
                this.P_Clear();

                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.ProcEnd, "btn_Clear_Click End", "btn_Clear_Click");
            }
            catch (Exception ex)
            {
                //ログ書込み
                G_CommonClassSystem.G_System_SetLog(G_CommonDefine.LogMode.Error, "btn_Clear_Click Error" + ex.Message, "btn_Clear_Click");

                //エラーメッセージ表示
                string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);
                MessageBox.Show("btn_Clear_Click" + ex.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
